package com.yun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.dao.QuestionDao;
import com.yun.dao.ResultDao;
import com.yun.dao.TesterDao;
import com.yun.entity.Question;
import com.yun.entity.Result;
import com.yun.entity.Tester;
import com.yun.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Result)表服务实现类
 *
 * @author makejava
 * @since 2021-11-27 15:56:38
 */
@Service("resultService")
public class ResultServiceImpl extends ServiceImpl<ResultDao, Result> implements ResultService {
    @Resource
    HttpServletRequest httpServletRequest;

    @Resource
    private ResultDao resultDao;

    @Resource
    private QuestionDao questionDao;

    @Resource
    private TesterDao testerDao;

    @Override
    public boolean save(List<String> list) {
        Map<Integer,String> map = new HashMap<>();
        Tester tester = new Tester();
        int red=0;
        int blue =0;
        int green = 0;
        int yellow=0;
        List<Question> questions = questionDao.selectList(new QueryWrapper<>(new Question()));
        for (int i = 0; i < list.size(); i++) {
            map.put(questions.get(i).getId(),list.get(i));
            if((questions.get(i).getType()==1&&i<15&&list.get(i).equals("A"))||(questions.get(i).getType()==2&&i>=15&&list.get(i).equals("D"))){
                red++;
            }
            if((questions.get(i).getType()==1&&i<15&&list.get(i).equals("B"))||(questions.get(i).getType()==2&&i>=15&&list.get(i).equals("C"))){
                blue++;
            }
            if((questions.get(i).getType()==1&&i<15&&list.get(i).equals("C"))||(questions.get(i).getType()==2&&i>=15&&list.get(i).equals("B"))){
                yellow++;
            }
            if((questions.get(i).getType()==1&&i<15&&list.get(i).equals("D"))||(questions.get(i).getType()==2&&i>=15&&list.get(i).equals("A"))){
                green++;
            }
        }
        for (Map.Entry<Integer, String> integerStringEntry : map.entrySet()) {
            System.out.println(integerStringEntry.getKey()+":"+integerStringEntry.getValue());
        }

        tester.setRedCharacter(red);
        tester.setBlueCharacter(blue);
        tester.setGreenCharacter(green);
        tester.setYellowCharacter(yellow);
        HttpSession session = httpServletRequest.getSession();
        Tester sessiontester = (Tester) session.getAttribute("tester");
        //session取得tester对象的id放进去
        Tester phone = testerDao.checkPhone(sessiontester.getPhone());
        int id = phone.getId();
        int insert = resultDao.insert(map,id);
        Tester tester1 = new Tester();
        tester1.setId(id);
        testerDao.update(tester,new UpdateWrapper<>(tester1));
        return insert>0?true:false;
    }
}

